﻿using System.ComponentModel;
using CommunityToolkit.Mvvm.ComponentModel;
using MG.SmartHMI.Model.Common;
using MG.Util;

namespace MG.SmartHMI.Model.VO
{
    public partial class RegisterConfigVO : ObservableObject
    {
        public int Id { get; set; }

        public bool IsDelete { get; set; }
        public DateTime UpdateTime { get => DateTime.Now; }

        public EnumControlType ControlType
        {
            get
            {
                foreach (var item in Enum.GetValues<EnumControlType>())
                {
                    if (item.GetDescrip().Equals(SControlType)) return item;
                }
                return EnumControlType.NormButton;
            }
            set => SControlType = value.GetDescription();
        }

        public EnumDataType DataType
        {
            get
            {
                foreach (var item in Enum.GetValues<EnumDataType>())
                {
                    if (item.GetDescrip().Equals(SDataType)) return item;
                }
                return EnumDataType.BOOL;
            }
            set => SDataType = value.GetDescription();
        }

        public EnumTriggerType TriggerType
        {
            get
            {
                foreach (var item in Enum.GetValues<EnumTriggerType>())
                {
                    if (item.GetDescrip().Equals(STriggerType)) return item;
                }
                return EnumTriggerType.HightLevel;
            }
            set => STriggerType = value.GetDescription();
        }

        [ObservableProperty]
        [Description("名称")]
        private string name = "新建";

        [ObservableProperty]
        [Description("排列顺序")]
        private int sortLevel;

        [ObservableProperty]
        [Description("控件类型")]
        private string sControlType = EnumControlType.Switch.GetDescription();

        [ObservableProperty]
        [Description("地址")]
        private string address;

        [ObservableProperty]
        [Description("数据类型")]
        private string sDataType = EnumDataType.USHORT.GetDescription();

        [ObservableProperty]
        [Description("触发方式")]
        private string sTriggerType = EnumTriggerType.HightLevel.GetDescription();

        [ObservableProperty]
        [Description("有效值")]
        private string effectValue;

        [ObservableProperty]
        [Description("字符长度")]
        private int strLen = 0;

        [ObservableProperty]
        [Description("显示在主界面")]
        private bool disp_MainView;

        [ObservableProperty]
        [Description("显示在配方界面")]
        private bool disp_SlnView;

        [ObservableProperty]
        [Description("显示在调试界面")]
        private bool disp_DebugView;

        [ObservableProperty]
        private string testValue = "0";

        [ObservableProperty]
        private bool enableTriggerType;

        [ObservableProperty]
        private bool enableDataType;

        [ObservableProperty]
        private bool enableStrLen;

        partial void OnSDataTypeChanged(string value)
        {
            EnableTriggerType = value.Equals(EnumDataType.BOOL.GetDescrip());
            if (!EnableTriggerType) STriggerType = EnumTriggerType.LowLevel.GetDescrip();
            EnableStrLen = value.Equals(EnumDataType.STRING.GetDescrip());
            if (!EnableStrLen) StrLen = 0;
        }

        partial void OnSControlTypeChanged(string value)
        {
            var list = Enum.GetValues<EnumControlType>().Where(t => t.GetDescrip().Equals(value));
            if (list == null || list.Count() < 1) return;

            switch (list.First())
            {
                case EnumControlType.InputBox:
                    EnableDataType = true;
                    EnableTriggerType = false;
                    break;

                case EnumControlType.Switch:
                    SDataType = EnumDataType.BOOL.GetDescrip();
                    EnableDataType = false;
                    STriggerType = EnumTriggerType.HightLevel.GetDescrip();
                    EnableTriggerType = false;
                    break;

                case EnumControlType.InputSignal:
                    SDataType = EnumDataType.BOOL.GetDescrip();
                    EnableDataType = false;
                    EnableTriggerType = false; break;
                case EnumControlType.CheckBox:
                    SDataType = EnumDataType.BOOL.GetDescrip();
                    STriggerType = EnumTriggerType.HightLevel.GetDescrip();
                    EnableDataType = false;
                    EnableTriggerType = false;
                    break;

                case EnumControlType.DispDataBox:
                    EnableDataType = true;
                    EnableTriggerType = false;
                    break;

                case EnumControlType.NormButton:
                case EnumControlType.StartButton:
                case EnumControlType.ResetButton:
                case EnumControlType.EmgButton:
                case EnumControlType.StopButton:
                case EnumControlType.PauseButton:
                    EnableDataType = true;
                    EnableTriggerType = true; break;
                default:
                    break;
            }
        }
    }
}